<style lang="less">
  @import '../../styles/common.less';
</style>

<template>
<div style="width: 480px; margin-top: 20px;">
  <Form ref="adminData" :model="adminData" :rules="ruleValidate" :label-width="110">
    <FormItem label="用户名" prop="userName">
      <Input v-model="adminData.userName" placeholder="请输入用户名"></Input>
    </FormItem>
    <FormItem label="密码" prop="passWord">
      <Input v-model="adminData.passWord" placeholder="请输入密码"></Input>
    </FormItem>
    <FormItem label="角色" prop="stduentSex">
      <Select v-model="adminData.roleId" style="width:200px">
        <Option v-for="item in roleList" :value="item.value" :key="item.value">{{ item.label }}</Option>
      </Select>
    </FormItem>


    <FormItem>
      <Button type="primary" @click="handleSubmit('adminData')">提交</Button>
      <Button type="ghost" @click="handleReset('adminData')">清空</Button>
    </FormItem>
  </Form>
        </div>
</template>

<script>
    export default {
        name: "addstduent",
        data(){
          return{
            adminData:{
              userName:'',
              passWord:'',
              roleId:3,
              id:0
            },
            ruleValidate:{
              stduentName:[
                {required: true,message:'请输入姓名',trigger:'blur'}
              ],
              stduentNo:[
                {required: true,message:'请输入学号',trigger:'blur'}
              ]
            },
            roleList:[
              {
                value:1,
                label:'管理员'
              },
              {
                value:2,
                label:'老师'
              },
              {
                value:3,
                label:'学生'
              },
              {
                value:4,
                label:'编辑'
              }
            ]
          }
        },
        methods:{
          handleSubmit(name){
            this.$refs[name].validate((valid) => {
              if (valid) {
                let dataArr = {
                  userName:this.adminData.userName,
                  passWord:this.adminData.passWord,
                  roleId:this.adminData.roleId,
                  id:this.adminData.id
                };

                console.log(dataArr);
                if(this.adminData.id > 0){
                  this.$http.post('/school/updStduent', dataArr).then(res => {
                    this.$Message.success('更新成功!');
                    this.handleReset(name);
                  });
                }else {
                  this.$http.post('/school/addUser', dataArr).then(res => {
                    this.$Message.success('添加成功!');
                    this.handleReset(name);
                  });
                }
              }
            })
          },
          handleReset (name) {
            this.$refs[name].resetFields();
          }
        },
      mounted () {
        console.log(this.$route.query.id);
        if(this.$route.query.id == undefined) return;
        this.$http.get('/school/getStduentById?id='+this.$route.query.id).then(res=>{
          console.log(res);
          var dt = res.data.data;
          this.stduentData.id = dt.id;
          this.stduentData.stduentName = dt.stduentName;
          this.stduentData.stduentNo = dt.stduentNo;
          this.stduentData.stduentSex = dt.stduentSex;
          this.stduentData.stduentPhone = dt.stduentPhone;
          this.stduentData.stduentIdCard = dt.stduentIdCard;
          this.stduentData.stduentCollege = dt.stduentCollege;
          this.stduentData.stduentClass = dt.stduentClass;

        });
      },
      created () {

      }
    }
</script>

<style scoped>

</style>
